package com.cloud.sso.common.util;

import com.cloud.sso.common.exception.AppException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

/**
 *
 */

/**
 * 统一响应结果
 *
 * @author wanghuifeng
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class JsonContent implements Serializable {

    private int code;

    private String message;

    private Object data;

    private Date now;

    private boolean success = false;

    private Object exData;

    /**
     * 正常返回
     */
    public static JsonContent success(Object object) {
//        if(object==null){
//            return new JsonContent(200, "操作成功", object, new Date());
//        }
//        if (object instanceof Collection) {
//            List<Object> list = (List<Object>) object;
//            handleAttachment(list);
//        }
//
//        if (object instanceof PageInfo) {
//            List<Object> list = ((PageInfo)object).getList();
//            handleAttachment(list);
//        }
//
//        if (object instanceof BaseEntity && object instanceof AttachmentAble) {
//            List<Object> list =Lists.newArrayList();
//            list.add(object);
//            handleAttachment(list);
//        }

        return new JsonContent(200, "操作成功！", object, new Date(), true, null);
    }

    public static JsonContent success() {
        return success(null);
    }

    /**
     * 错误返回
     */
    public static JsonContent error(Exception exception) {
        return new JsonContent(-1, exception.getMessage(), null, new Date(), false, null);
    }

    public static JsonContent error(int code, Exception e) {
        return new JsonContent(code, e.getMessage(), null, new Date(), false, null);
    }


    /**
     * 异常信息
     */
    public static JsonContent error(String errmsg) {
        return error(new AppException(errmsg));
    }

}
